//
//  HJStatusTableViewCell.swift
//  WeiBo
//
//  Created by 韩俊 on 15/12/10.
//  Copyright © 2015年 HanJun. All rights reserved.
//

import UIKit
import SnapKit

// cell 内部子控件共同的间距
let HJStatusCellMargin: CGFloat = 10
// 昵称的字体大小
let HJStatusNameFontSize: CGFloat = 14
// 时间与来源的字体大小
let HJStatusTimeFontSize: CGFloat = 10
// 内容字体大小
let HJStatusContentFontSize: CGFloat = 15

class HJStatusTableViewCell: UITableViewCell {
    
    var toolBarConstraint : Constraint?
    
    var statusViewModel:HJStatusViewModel?{
        
        didSet{
            
            originalView.statusViewModel = statusViewModel
            statusToolBar.statusViewModel = statusViewModel
            
            self.toolBarConstraint?.uninstall()
            
            if statusViewModel?.status?.retweeted_status != nil {
                
                retweetView.hidden = false
                retweetView.statusViewModel = statusViewModel
                
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                   self.toolBarConstraint = make.top.equalTo(retweetView.snp_bottom).offset(HJStatusCellMargin).constraint
                    
                })
                
            }else{
                
                retweetView.hidden = true
                
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
            
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        //添加子控件
        contentView.addSubview(originalView)
        contentView.addSubview(statusToolBar)
        contentView.addSubview(retweetView)
        //添加约束
        
        originalView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)
          
            make.trailing.equalTo(contentView)
        }
        
        // 转发微博
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(originalView)
            make.top.equalTo(originalView.snp_bottom)
            make.trailing.equalTo(originalView)
            
        }
        // 底部 toolBar
        statusToolBar.snp_makeConstraints { (make) -> Void in
            
            self.toolBarConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.width.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        // 非常重要,而约束不能少
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(statusToolBar)
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
        }
        

    }
    
    
    // MARK: - 懒加载
    private lazy var  originalView: HJStatusOriginalView = {
        let view = HJStatusOriginalView()
        view.backgroundColor = UIColor.whiteColor()
        return view
    }()
    // 底部 toolBar
    private lazy var statusToolBar: HJStatusToolBar = HJStatusToolBar()
    // 转发微博 view
    private lazy var retweetView: HJStatusRetweetView = HJStatusRetweetView()
    
}
